package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.ResultInfo;
import com.itheima.reggie.domain.Category;
import com.itheima.reggie.domain.Dish;
import com.itheima.reggie.domain.Setmeal;
import com.itheima.reggie.service.DishService;
import com.itheima.reggie.service.SetmealService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

// 菜品管理
@RestController
public class DishController {
    @Autowired
    private DishService dishService;
    @Autowired
    private SetmealService setmealService;
    @GetMapping("/dish/page")
    public ResultInfo findByPage(@RequestParam(value = "page",defaultValue = "1") Integer pageNum,
                                 @RequestParam(value = "pageSize",defaultValue = "10") Integer pageSize,
                                 String name){//接收参数
        // 2.调用service查询
        Page<Dish> dishPage=dishService.findByPage(pageNum,pageSize,name);
        //返回结果
        return ResultInfo.success(dishPage);

    }
    //新增菜品
    @PostMapping("/dish")
    public ResultInfo save(@RequestBody Dish dish){
        //调用service
        dishService.save(dish);
        //返回结果
        return ResultInfo.success();

    }
    //修改回显菜品
    @GetMapping("/dish/{id}")
    public ResultInfo findById(@PathVariable("id") Long id){
    //调用service查询
        Dish dish=dishService.findById(id);
        //返回结果
        return ResultInfo.success(dish);

    }
    //修改菜品信息
    @PutMapping("/dish")
    public ResultInfo update(@RequestBody Dish dish){
        //调用service
        dishService.update(dish);
        //返回结果
        return ResultInfo.success();

    }
    //起售停售
    @PostMapping("/dish/status/{type}")
    public ResultInfo updateStatus(@PathVariable("type") Integer type, @RequestParam List<Long> ids){// list集合接收配合@RequestParam
        //调用service
        dishService.updateStatus(type,ids);
        //返回结果
        return ResultInfo.success();
    }

    // 菜品删除
    @DeleteMapping("/dish")
    public ResultInfo deleteBatchIds(@RequestParam List<Long> ids){ // 1.接收参数
        // 2.调用service
        dishService.deleteBatchIds(ids);
        // 3.返回成功
        return ResultInfo.success();
    }
    //新增套餐  回显
    @GetMapping("/dish/list")
    public ResultInfo findAll(Long categoryId ,String name){//接收参数
        //调用service
        List<Dish> dish=dishService.findAll(categoryId,name);
        //返回结果
        return ResultInfo.success(dish);

    }


    @PostMapping("/setmeal")
    public ResultInfo save(@RequestBody Setmeal setmeal){
        //调用service
        setmealService.save(setmeal);
        //返回结果
        return ResultInfo.success();

    }

}
